# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations
    
from gluon.storage import Storage
import string

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################

def userContribute( p ):

    found = False
       
    firstName = p.vars.firstNameInput if string.find( p.vars.firstNameInput, 'optional' ) < 0 else ''
    age = p.vars.ageInput if string.find( p.vars.ageInput, 'optional' ) < 0 else ''
    hometown = p.vars.hometownInput if string.find( p.vars.hometownInput, 'optional' ) < 0 else ''


    for word in [ 'sand nigger', 'camel jockey', 'towel head', 'diaper head', 'dot head', 'Ayy-Rab', 'Mooozlum' ]:

        for input in [ firstName, age, hometown, p.vars.contributeInput ]:

            if( string.find( input, word ) > -1 ):

                found = True



    if( found ):

        db.filteredContribution.insert( firstName = firstName,
                                        age = age,
                                        hometown = hometown,
                                        contribution = p.vars.contributeInput,
                                        question = p.question )

    else:
        
        db.kioskContribution.insert( firstName = firstName,
                                     age = age,
                                     hometown = hometown,
                                     contribution = p.vars.contributeInput,
                                     question = p.question )

    return dict( firstName = firstName,
                 age = age,
                 hometown = hometown,
                 contribution = p.vars.contributeInput )


def getResponseIndex():

    ids = db( ( db.kioskContribution.question == request.args[0] ) & ( db.kioskContribution.approved == True ) ).select( db.kioskContribution.id, orderby="datetime DESC" )

    return response.json( ids )


def getResponse():
    
    return response.json( db( db.kioskContribution.id == request.args[0] ).select()[0].as_dict() )

    

def index():
    return dict()

def twoA():
    return dict()

def threeA():
    return dict()

def fourA():
    return dict()

def fiveA():
    return dict()

def sixA():
    return userContribute( Storage( vars = request.vars, question = 'A' ) )

def sevenA():
    return dict()

def sevenB():
    return dict()

def sevenC():
    return dict()

def oneB():
    return dict()

def twoB():
    return dict()

def threeB():
    return dict()

def fourB():
    return dict()

def fiveB():
    return dict()

def sixB():
    return userContribute( Storage( vars = request.vars, question = 'B' ) )

def oneC():
    return dict()

def twoC():
    return dict()

def threeC():
    return dict()

def fourC():
    return dict()

def fiveC():
    return dict()

def sixC():
    return userContribute( Storage( vars = request.vars, question = 'C' ) )

def main():
    return dict()

def user():
    """
    exposes:
    http://..../[app]/default/user/login
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())


def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    return service()


@auth.requires_signature()
def data():
    """
    http://..../[app]/default/data/tables
    http://..../[app]/default/data/create/[table]
    http://..../[app]/default/data/read/[table]/[id]
    http://..../[app]/default/data/update/[table]/[id]
    http://..../[app]/default/data/delete/[table]/[id[
    http://..../[app]/default/data/select/[table]
    http://..../[app]/default/data/search/[table]
    but URLs bust be signed, i.e. linked with
      A('table',_href=URL('data/tables',user_signature=True))
    or with the signed load operator
      LOAD('default','data.load',args='tables',ajax=True,user_signature=True)
    """
    return dict(form=crud())
